<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleUpload">上传文件</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '下载',
              icon: 'ant-design:download-outlined',
              onClick: handleDownload.bind(null, record),
            },
            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
      <template #json="{ record }">
        <JsonPreview :data="record.payload" />
      </template>
    </BasicTable>
    <UploadModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { JsonPreview } from '/@/components/CodeEditor';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getAttachmentList, AttachmentDel } from '/@/api/demo/general';

  import { columns, searchFormSchema } from './data';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadByUrl } from '/@/utils/file/download';

  import UploadModal from './UploadModal.vue';

  export default defineComponent({
    name: 'QueueLog',
    components: { BasicTable, UploadModal, TableAction, JsonPreview },
    setup() {
      const { createMessage, createConfirm } = useMessage();
      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload, getSelectRowKeys }] = useTable({
        title: '消息队列',
        api: getAttachmentList,
        rowKey: 'id',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      function handleUpload() {
        openModal(true, {
          isUpdate: false,
        });
      }

      async function handleDownload(record: Recordable) {
        console.log('下载');
        //createMessage.warning('功能开发中');
        downloadByUrl({
          url: record.url,
          target: '_blank',
          fileName: record.filename,
        });
      }

      function handleDelete(record: Recordable) {
        console.log(getSelectRowKeys());
        createConfirm({
          iconType: 'warning',
          title: '是否确认删除',
          onOk: function () {
            //调用删除方法
            console.log('ok');
            console.log(record);
            var ids = [record.id];
            AttachmentDel(ids).then(() => {
              reload();
            });
          },
          onCancel: function () {
            console.log('cancel');
          },
        });
      }

      function handleSuccess({ isUpdate, values }) {
        reload();
      }

      return {
        registerTable,
        registerModal,
        handleUpload,
        handleDownload,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
